package com.aaa.Dao;

import com.aaa.Entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class UserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    private RowMapper<User> rowMapper = new RowMapper<User>() {
        public User mapRow(ResultSet resultSet, int i) throws SQLException {
            User user = new User();
            user.setCreate_date(resultSet.getString("create_date"));
            user.setId(resultSet.getLong("id"));
            user.setUsername(resultSet.getString("username"));
            user.setPassword(resultSet.getString("password"));
            return user;
        }
    };

    public User findUAndP(User user){
        String a = "1";
        String sql = "select id,username,password,create_date from users where username=? and password=?";
        List<User> query = jdbcTemplate.query(sql, rowMapper, user.getUsername(), user.getPassword());
        if(query.size()>0){
            return query.get(0);
        }else {
            return null;
        }

    }
}
